package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 云bms电池
 *
 * @author LMY
 * @create 2022-07-13 19:44
 */
@RestController
@RequestMapping("/bmsBattery")
public class BmsBatteryController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private BmsBatteryService bmsBatteryService;
    @Autowired
    private BmsAbnormalService bmsAbnormalService;
    @Autowired
    private PowerChangeLogExpandService powerChangeLogExpandService;
    @Autowired
    private BatteryService batteryService;
    @Autowired
    private AccountService accountService;

    /**
     * 查询电池状态列表
     *
     * @param token
     * @param bmsStat 参数
     * @return
     */
    @RequestMapping("/getBatteryType")
    public JsonResult<List<BmsBattery>> getBatteryType(@NotNull String token, BmsStat bmsStat) {
        AccountUser accountUser = tokenService.getAccount(token);
        bmsStat.setAccountId(accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId());

        List<BmsBattery> bmsBatteryList = bmsBatteryService.getBattery(bmsStat);

        return JsonResult.succ(bmsBatteryList);
    }


    /**
     * 健康度分布
     *
     * @param token
     * @param bmsStat 参数
     * @return
     */
    @RequestMapping("/health")
    public JsonResult<BmsBatteryHealthAnalyse> health(@NotNull String token, BmsStat bmsStat) {
        AccountUser accountUser = tokenService.getAccount(token);
        bmsStat.setAccountId(accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId());
        BmsBatteryHealthAnalyse bmsBatteryHealthAnalyse = bmsBatteryService.getHealth(bmsStat);
        return JsonResult.succ(bmsBatteryHealthAnalyse);
    }


    /**
     * 电池添加到bms
     *
     * @param token
     * @param batteryNO   电池编号
     * @param batch       批次号
     * @param adAccountId 区域id
     * @return
     */
    @RequestMapping("/insert")
    @OperateLog(module = ModuleEnum.BMSBATTERY, methods = MethodEnum.BMSBATTERY_ADD)
    public JsonResult insert(@NotNull String token, @NotNull String batteryNO, @NotNull Integer adAccountId, Integer batch) {
        AccountUser accountUser = tokenService.getAccount(token);

        Battery battery = batteryService.getByNO(batteryNO);

        if (!battery.getAccountId().equals(adAccountId)) {
            throw new BaseException("bms.accountId.adAccountId");
        }

        batteryService.auth(accountUser.getAgents(), battery.getBatteryId());

        bmsBatteryService.insert(accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId(), batteryNO, batch);
        return JsonResult.succ();
    }

    /**
     * 电池删除云bms
     *
     * @param token
     * @param batteryNO
     * @return
     */
    @RequestMapping("/delete")
    @OperateLog(module = ModuleEnum.BMSBATTERY, methods = MethodEnum.BMSBATTERY_DEL)
    public JsonResult delete(@NotNull String token, @NotNull String batteryNO) {
        AccountUser accountUser = tokenService.getAccount(token);

        Battery battery = batteryService.getByNO(batteryNO);

        batteryService.auth(accountUser.getAgents(), battery.getBatteryId());

        bmsBatteryService.delete(batteryNO);
        return JsonResult.succ();
    }

    /**
     * 批量电池添加到bms
     *
     * @param token
     * @param batteryNOs  电池编号
     * @param batch       批次号
     * @param adAccountId 区域id
     * @return
     */
    @RequestMapping("/insertBatch")
    @OperateLog(module = ModuleEnum.BMSBATTERY, methods = MethodEnum.BMSBATTERY_ADD)
    public JsonResult insertBatch(@NotNull String token, @NotNull @RequestParam(value = "batteryNOs", required = false) List<String> batteryNOs, @NotNull Integer adAccountId, Integer batch) {
        AccountUser accountUser = tokenService.getAccount(token);


        bmsBatteryService.auth(batteryNOs, adAccountId);

        Battery battery = batteryService.getByNO(batteryNOs.get(0));

        batteryService.auth(accountUser.getAgents(), battery.getBatteryId());

        bmsBatteryService.insertBatch(accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId(), batteryNOs, batch);
        return JsonResult.succ();
    }


    /**
     * 查询电池异常
     *
     * @param token
     * @param adAccountId  区域id
     * @param abnormalType 异常类型
     * @param handle       处理状态 0：未处理，1：已处理
     * @param batteryNO    电池编号
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @return
     */
    @RequestMapping("/abnormal")
    public JsonResult<List<BmsAbnormal>> abnormal(@NotNull String token, @NotNull Integer adAccountId, Integer abnormalType,
                                                  Integer handle, String batteryNO, @NotNull String startTime, @NotNull String endTime) {
        List<BmsAbnormal> bmsAbnormals = bmsAbnormalService.getAbnormal(adAccountId, abnormalType, handle, batteryNO, startTime, endTime);
        return JsonResult.succ(bmsAbnormals);
    }

    /**
     * 批量处理电池异常数据
     *
     * @param token
     * @param adAccountId  区域id
     * @param abnormalType 异常类型
     * @param batteryNO    电池编号
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @return
     */
    @RequestMapping("/abnormalGroup")
    @OperateLog(module = ModuleEnum.BMSBATTERY, methods = MethodEnum.BMSBATTERYH_UPDATE)
    public JsonResult<BmsAbnormalGroup> abnormalGroup(@NotNull String token, @NotNull Integer adAccountId, Integer abnormalType
            , String batteryNO, @NotNull String startTime, @NotNull String endTime) {
        BmsAbnormalGroup bmsAbnormalGroups = bmsAbnormalService.abnormalGroup(adAccountId, abnormalType, batteryNO, startTime, endTime);
        return JsonResult.succ(bmsAbnormalGroups);
    }

    /**
     * 批量处理电池异常
     *
     * @param token
     * @param adAccountId  区域id
     * @param abnormalType 异常类型
     * @param batteryNO    电池编号
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @return
     */
    @RequestMapping("/handle")
    @OperateLog(module = ModuleEnum.BMSBATTERY, methods = MethodEnum.BMSBATTERYH_UPDATE)
    public JsonResult handle(@NotNull String token, @NotNull Integer adAccountId, Integer abnormalType,
                             String batteryNO, @NotNull String startTime, @NotNull String endTime) {
        bmsAbnormalService.handle(adAccountId, abnormalType, batteryNO, startTime, endTime);
        return JsonResult.succ();
    }


    /**
     * 查询电池记录
     *
     * @param token
     * @param adAccountId 区域id
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @param batteryNO   电池编号
     * @return
     */
    @RequestMapping("/getBatteryLog")
    public JsonResult<List<PowerChangeLogExpand>> getBatteryLog(@NotNull String token, @NotNull Integer adAccountId,
                                                                @NotNull String startTime, @NotNull String endTime, String batteryNO) {
        List<PowerChangeLogExpand> powerChangeLogExpands = powerChangeLogExpandService.getBatteryLog(adAccountId, startTime, endTime, batteryNO);

        return JsonResult.succ(powerChangeLogExpands);
    }

}
